package com.xuetang9.fujian.net.server;

import com.xuetang9.fujian.net.request.AbstractRequestHandler;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.HashMap;

/**
 * @describe:
 * @author:fujian
 * @date:2021/6/12
 * @Version: V1.0
 * @Package: com.xuetang9.fujian.net.server
 */
@SuppressWarnings("all")
public class SocketHandler implements Runnable{

    private Socket client;
    private ECQServer server;
    private String key;
    private int status;
    public static final int CLOSE = 1;
    public static final int RUNNING=2;
    private ObjectInputStream inputStream;
    private ObjectOutputStream outputStream;

    public SocketHandler(Socket client, ECQServer server){
        this.client = client;
        this.server = server;
    }

    @Override
    public void run() {
        try {
            inputStream = new ObjectInputStream(client.getInputStream());
            outputStream = new ObjectOutputStream(client.getOutputStream());
            setStatus(RUNNING);
            Object data = null;
            while (getStatus() == RUNNING && (data = inputStream.readObject()) != null ){
                if (data instanceof HashMap){
                    // deal with
                    handleMessage((HashMap<String,Object>) data);
                }else {
                    System.out.println("can't deal this message");
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private void handleMessage(HashMap<String, Object> message) {
        String handlerClassName = message.get("handlerClass").toString();
        try {
            Class<?> handlerClass = Class.forName(handlerClassName);
            Object handlerObject = handlerClass.newInstance();
            if (handlerObject instanceof AbstractRequestHandler){
                AbstractRequestHandler requestHandler = (AbstractRequestHandler) handlerObject;
                message.remove("handlerClass");
                requestHandler.handleRequest(message,this,this.server);
            }
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public void sendMessage(HashMap<String,Object> message){
        try {
            outputStream.writeObject(message);
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public void close(){
        try {
            outputStream.close();
            inputStream.close();
            client.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
